package com.hxd.otc.module.my.fragment;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.LazyFragment;
import com.hxd.otc.module.home.fragment.HomePageFragment;
import com.hxd.otc.module.my.adapter.StableCurrencyAdapter;
import com.hxd.otc.module.my.contract.StableCurrencyContract;
import com.hxd.otc.module.my.presenter.StableCurrencyPresenter;
import com.hxd.otc.module.order.bean.StableCurrencyBean;

import butterknife.BindView;

/**
 * 作  者 :  zjl
 * 时  间 :  2018/12/26 15:07
 * 描  述 :  ${稳定比}
 */
public class StableCurrencyFragment extends LazyFragment<StableCurrencyPresenter> implements StableCurrencyContract.View {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    View errorView;

    View notDataView;

    TextView tvRefresh;

    StableCurrencyAdapter adapter;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    protected void initData() {
        initAdapter();
        recyclerView.setNestedScrollingEnabled(false);
        refreshData();
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        notDataView = getLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) recyclerView.getParent(), false);
        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) recyclerView.getParent(), false);
        tvRefresh = errorView.findViewById(R.id.tv_refresh);
        tvRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                refreshData();
            }
        });

    }

    public void refreshData() {
        mPresenter.doLoadData();
    }

    private void initAdapter() {
        if (adapter == null) {
            adapter = new StableCurrencyAdapter();
        }
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_stable_currency;
    }

    @Override
    protected StableCurrencyPresenter createPresenter() {
        return new StableCurrencyPresenter(this);
    }

    @Override
    public void onShowNoMore() {
        if (getParentFragment() != null)
            ((HomePageFragment) getParentFragment()).onShowNoMore();
    }

    @Override
    public void onSetAdapter(@NonNull StableCurrencyBean bean) {
        if (bean == null || bean.otcEndorses.size() <= 0) {
            adapter.setEmptyView(notDataView);
            return;
        }
        if (getParentFragment() != null)
            ((HomePageFragment) getParentFragment()).setProgremme(bean.programme, bean.EUCaurrentAmount, bean.EUCaurrentAmountTax);

        if (adapter != null && bean.otcEndorses != null)
            adapter.setNewData(bean.otcEndorses);
    }

    @Override
    public void onError(int code) {
        super.onError(code);
        onShowNoMore();
        if (adapter != null && errorView != null)
            adapter.setEmptyView(errorView);
    }
}
